	
	boolean regrouping = false;
	boolean hasPickedCard = false;

	void UpdateTurn(int key)
	{
		switch(key)
		{
			case KEYS.KEY_STAR:
		
				switch(cPlayer.roundType)
				{
					case INGAME.ROUND_TYPE_PUT_ARMIES:
						int totalPerContinent = 0;
					
						if(cCountry.selectedCountry != cCountry.NONE_SELECTED && tegCountries[cCountry.selectedCountry].owner == cPlayer.turn)
						{
							boolean hasContinent = false;
							
							for(int i = COUNTRY_INFO.NORTH_AMERICA; i < COUNTRY_INFO.TOTAL_CONTINENTS; i++)
							{
								if(hasContinent(i) && tegCountries[firstCountryOfContinent(i)].owner == cPlayer.turn)
								{
									totalPerContinent = totalPerContinent + cCountry.continentArmies[i];
									if(tegCountries[cCountry.selectedCountry].continent == i)
									{
										hasContinent = true;
									}
								}
							}
							
							if(!hasContinent && tegPlayers[cPlayer.turn].armies > 0)
							{
								tegCountries[cCountry.selectedCountry].armies++;
								tegPlayers[cPlayer.turn].armies--;
							}
							else
							{
								if(cCountry.continentArmies[tegCountries[cCountry.selectedCountry].continent] > 0)
								{
									tegCountries[cCountry.selectedCountry].armies++;
									cCountry.continentArmies[tegCountries[cCountry.selectedCountry].continent]--;
									totalPerContinent--;
								}
								else if(tegPlayers[cPlayer.turn].armies > 0)
								{
									tegCountries[cCountry.selectedCountry].armies++;
									tegPlayers[cPlayer.turn].armies--;
								}
							}
						}
						
						if(tegPlayers[cPlayer.turn].armies == 0 && totalPerContinent == 0)
						{
							cCountry.selectedCountry = cCountry.NONE_SELECTED;
							
							messageId = OPTIONS.MESSAGE_ID_END_TURN;
							messageShowStart = 1;
							
							do
							{
								cPlayer.turn++;
							
								if(cPlayer.turn > rules[RULES.MATCH_PLAYERS] - 1)
								{
									cPlayer.turn -= rules[RULES.MATCH_PLAYERS];
								}
								
								if(cPlayer.turn == cPlayer.firstRoundTurn)
									break;
							}
							while(playerCountries(cPlayer.turn) == 0);
							
							shownPlayerInfo = cPlayer.turn;
							regrouping = false;
							canPickCard = false;
							
							if(cPlayer.turn == cPlayer.firstRoundTurn)
							{								
								if(cPlayer.turn > rules[RULES.MATCH_PLAYERS] - 1)
								{
									cPlayer.turn -= rules[RULES.MATCH_PLAYERS];
								}
								
								cPlayer.firstRoundTurn = cPlayer.turn;
								shownPlayerInfo = cPlayer.turn;
									
								cPlayer.roundType = INGAME.ROUND_TYPE_ATTACK;
							}
							
							if(isOptionEnabled(OPTIONS.AUTOSAVE))
								saveGame();
						}
						
					break;
					
					case INGAME.ROUND_TYPE_ATTACK:
						
						if(cCountry.selectedCountry != cCountry.NONE_SELECTED && cCountry.defendingCountry != cCountry.NONE_SELECTED)
						{
							if(!regrouping && !hasPickedCard)
								attack(cCountry.selectedCountry, cCountry.defendingCountry);
							
							if(tegCountries[cCountry.selectedCountry].owner == tegCountries[cCountry.defendingCountry].owner && !conqueredCountry && !hasPickedCard
								&& tegCountries[cCountry.selectedCountry].owner == cPlayer.turn)
							{
								regrouping = true;
								
								if(messageId == OPTIONS.MESSAGE_ID_IDLE)
								{
									messageId = OPTIONS.MESSAGE_ID_REGROUPED;
									messageShowStart = System.currentTimeMillis();
								}
								
								if(tegCountries[cCountry.selectedCountry].regroupableArmies > 0)
								{
									tegCountries[cCountry.selectedCountry].regroupableArmies--;
									tegCountries[cCountry.selectedCountry].armies--;
									tegCountries[cCountry.defendingCountry].armies++;
								}
							}
						}
						
					break;
				}
				
			break;
			
			case KEYS.KEY_POUND:
			
				switch(cPlayer.roundType)
				{
					case INGAME.ROUND_TYPE_ATTACK:
						
						cCountry.selectedCountry = cCountry.NONE_SELECTED;
						cCountry.defendingCountry = cCountry.NONE_SELECTED;
						
						messageId = OPTIONS.MESSAGE_ID_END_TURN;
						messageShowStart = 1;
						
						do
						{
							cPlayer.turn++;
							
							if(cPlayer.turn > rules[RULES.MATCH_PLAYERS] - 1)
							{
								cPlayer.turn -= rules[RULES.MATCH_PLAYERS];
							}
							
							if(cPlayer.turn == cPlayer.firstRoundTurn)
									break;
						}
						while(playerCountries(cPlayer.turn) == 0);
						
						shownPlayerInfo = cPlayer.turn;
						regrouping = false;
						canPickCard = false;
						hasPickedCard = false;
						
						if(cPlayer.turn == cPlayer.firstRoundTurn)
						{
							cPlayer.turn++;
							
							checkForWin();
							
							if(cPlayer.turn > rules[RULES.MATCH_PLAYERS] - 1)
							{
								cPlayer.turn -= rules[RULES.MATCH_PLAYERS];
							}
							
							cPlayer.firstRoundTurn = cPlayer.turn;
							shownPlayerInfo = cPlayer.turn;
							
							for(int i = 0; i < rules[RULES.MATCH_PLAYERS]; i++)
							{
								tegPlayers[i].armies = calcPlayerArmies(i);
							}
								
							cPlayer.roundType = INGAME.ROUND_TYPE_PUT_ARMIES;
						}
							
						if(isOptionEnabled(OPTIONS.AUTOSAVE))
							saveGame();
						
					break;
				}
				
			break;
			
			case KEYS.KEY_6:
				if(canPickCard && !hasPickedCard)
					UpdatePickCard();
					
			break;
		}
	}
	
	int[] attackingDices;
	int[] defendingDices;
	boolean conqueredCountry;
	boolean showBattleResults;
	boolean canPickCard;
	
	public void attack(int attackingCountry, int defendingCountry)
	{
		if(tegCountries[attackingCountry].owner == tegCountries[defendingCountry].owner)
			return;
		
		if(tegCountries[attackingCountry].armies > 1)
		{
			attackingDices = new int[(tegCountries[attackingCountry].armies - 1 > 3)? 3 : (tegCountries[attackingCountry].armies - 1)];
			defendingDices = new int[(tegCountries[defendingCountry].armies > rules[RULES.MAX_DEFENDING_ARMIES])? rules[RULES.MAX_DEFENDING_ARMIES] : tegCountries[defendingCountry].armies];
			
			for(int i = 0; i < attackingDices.length; i++)
			{
				attackingDices[i] = rollDice();
			}
			
			sortDices(attackingDices);
			
			for(int j = 0; j < defendingDices.length; j++)
			{
				defendingDices[j] = rollDice();
			}
			
			sortDices(defendingDices);
			
			for(int m = 0; m < ((attackingDices.length > defendingDices.length)? defendingDices.length : attackingDices.length); m++)
			{
				if(attackingDices[m] > defendingDices[m])
				{
					tegCountries[defendingCountry].armies--;
				}
				else
				{
					tegCountries[attackingCountry].armies--;
				}
			}
			
			if(tegCountries[defendingCountry].armies == 0)
			{
				// Check win for destroy objectives
				if(rules[RULES.DESTROY_OBJECTIVES] == 1 && tegPlayers[tegCountries[attackingCountry].owner].objectiveId >= RULES.OBJECTIVES.length - 6)
				{
					boolean destroyable = false;
				
					for(int t = 0; t < tegPlayers.length; t++)
					{
						if(tegPlayers[t].color == RULES.OBJECTIVES[tegPlayers[tegCountries[attackingCountry].owner].objectiveId][0])
						{
							destroyable = true;
							break;
						}
					}
					
					if(destroyable && playerCountries(tegCountries[defendingCountry].owner) == 1)
					{
						winner = tegCountries[attackingCountry].owner;
						someoneWon = true;
						SetIngameState(STATES.INGAME_MATCH_END);
					}
					else if(!destroyable)
					{
						int newVictim = -1;
						
						if(!(tegCountries[attackingCountry].owner + 1 == tegPlayers.length))
							newVictim = 0;
						else
							newVictim = tegCountries[attackingCountry].owner + 1;
						
						if(tegCountries[defendingCountry].owner == newVictim && playerCountries(tegCountries[defendingCountry].owner) == 1)
						{
							winner = tegCountries[attackingCountry].owner;
							someoneWon = true;
							SetIngameState(STATES.INGAME_MATCH_END);
						}
					}
				}
				// Check win end
				
				if(countriesInContinent(tegCountries[attackingCountry].owner, tegCountries[defendingCountry].continent) == (COUNTRY_INFO.TOTAL_COUNTRIES_IN_CONTINENT[tegCountries[defendingCountry].continent] - 1))
				{
					messageId = OPTIONS.MESSAGE_ID_CONTINENT;
					messageShowStart = 1;
				}
				
				tegPlayers[tegCountries[defendingCountry].owner].ownedCountries--;
				tegCountries[defendingCountry].owner = tegCountries[attackingCountry].owner;
				tegPlayers[tegCountries[attackingCountry].owner].ownedCountries++;
				tegPlayers[tegCountries[attackingCountry].owner].lastConqueredCountry = defendingCountry;
				conqueredCountry = true;
				canPickCard = true;
			}
			
			showBattleResults = true;
		}
	}
	
	public void sortDices(int[] dices)
	{
	   for (int k = 0; k < dices.length - 1; k++)
	   {
	      boolean isSorted = true;

	      for (int i = 1; i < dices.length - k; i++)
	      {
	         if (dices[i] > dices[i - 1])
	         {
	            int buf = dices[i];
	            dices[i] = dices[i - 1];
	            dices[i - 1] = buf;

	            isSorted = false;

	         }
	      }

	      if (isSorted)
	         break;
	   }
	}
	
	public int calcPlayerArmies(int player)
	{
		int playerCountries = 0;
		
		for(int i = COUNTRY_INFO.NORTH_AMERICA; i < COUNTRY_INFO.TOTAL_CONTINENTS; i++)
		{
			if(hasContinent(i))
			{
				cCountry.continentArmies[i] = COUNTRY_INFO.ARMIES_PER_CONTINENT[i];
			}
		}
		
		for(int j = 0; j < COUNTRY_INFO.TOTAL_COUNTRIES; j++)
		{
			if(tegCountries[j].owner == player)
			{
				playerCountries++;
			}
		}
		
		if(playerCountries / rules[RULES.COUNTRIES_DIVISOR] >= 3)
			return playerCountries / rules[RULES.COUNTRIES_DIVISOR];
		else
			return 3;
	}
	
	public int playerCountries(int player)
	{
		int totalCountries = 0;
	
		for(int i = 0; i < COUNTRY_INFO.TOTAL_COUNTRIES; i++)
		{
			if(tegCountries[i].owner == player)
			{
				totalCountries++;
			}
		}
		
		return totalCountries;
	}
	
	public boolean hasContinent(int continentId)
	{
		int firstCountry = 0;
		int lastCountry = 0;
		
		boolean itHas = true;
		
		switch(continentId)
		{
			case COUNTRY_INFO.ASIA:
				firstCountry = COUNTRY_INFO.ARAL;
				lastCountry = COUNTRY_INFO.JAPON;
			break;
			case COUNTRY_INFO.NORTH_AMERICA:
				firstCountry = COUNTRY_INFO.ALASKA;
				lastCountry = COUNTRY_INFO.MEXICO;
			break;
			case COUNTRY_INFO.SOUTH_AMERICA:
				firstCountry = COUNTRY_INFO.COLOMBIA;
				lastCountry = COUNTRY_INFO.URUGUAY;
			break;
			case COUNTRY_INFO.AFRICA:
				firstCountry = COUNTRY_INFO.SAHARA;
				lastCountry = COUNTRY_INFO.MADAGASCAR;
			break;
			case COUNTRY_INFO.OCEANIA:
				firstCountry = COUNTRY_INFO.SUMATRA;
				lastCountry = COUNTRY_INFO.JAVA;
			break;
			case COUNTRY_INFO.EUROPE:
				firstCountry = COUNTRY_INFO.ISLANDIA;
				lastCountry = COUNTRY_INFO.RUSIA;
			break;
		}
		
		for(int i = firstCountry; i <= lastCountry; i++)
		{
			if(tegCountries[i].owner != tegCountries[firstCountry].owner)
			{
				itHas = false;
				break;
			}
		}
		
		return itHas;
	}
	
	public boolean playerHasContinent(int player, int continentId)
	{
		int firstCountry = 0;
		int lastCountry = 0;
		
		boolean itHas = true;
		
		switch(continentId)
		{
			case COUNTRY_INFO.ASIA:
				firstCountry = COUNTRY_INFO.ARAL;
				lastCountry = COUNTRY_INFO.JAPON;
			break;
			case COUNTRY_INFO.NORTH_AMERICA:
				firstCountry = COUNTRY_INFO.ALASKA;
				lastCountry = COUNTRY_INFO.MEXICO;
			break;
			case COUNTRY_INFO.SOUTH_AMERICA:
				firstCountry = COUNTRY_INFO.COLOMBIA;
				lastCountry = COUNTRY_INFO.URUGUAY;
			break;
			case COUNTRY_INFO.AFRICA:
				firstCountry = COUNTRY_INFO.SAHARA;
				lastCountry = COUNTRY_INFO.MADAGASCAR;
			break;
			case COUNTRY_INFO.OCEANIA:
				firstCountry = COUNTRY_INFO.SUMATRA;
				lastCountry = COUNTRY_INFO.JAVA;
			break;
			case COUNTRY_INFO.EUROPE:
				firstCountry = COUNTRY_INFO.ISLANDIA;
				lastCountry = COUNTRY_INFO.RUSIA;
			break;
		}
		
		for(int i = firstCountry; i <= lastCountry; i++)
		{
			if((tegCountries[i].owner != tegCountries[firstCountry].owner) || (player != tegCountries[firstCountry].owner))
			{
				itHas = false;
				break;
			}
		}
		
		return itHas;
	}
	
	public int firstCountryOfContinent(int continentId)
	{
		switch(continentId)
		{
			case COUNTRY_INFO.ASIA:
				return COUNTRY_INFO.ARAL;
			case COUNTRY_INFO.NORTH_AMERICA:
				return COUNTRY_INFO.ALASKA;
			case COUNTRY_INFO.SOUTH_AMERICA:
				return COUNTRY_INFO.COLOMBIA;
			case COUNTRY_INFO.AFRICA:
				return COUNTRY_INFO.SAHARA;
			case COUNTRY_INFO.OCEANIA:
				return COUNTRY_INFO.SUMATRA;
			case COUNTRY_INFO.EUROPE:
				return COUNTRY_INFO.ISLANDIA;
		}
		
		return 0;
	}